package structures;

import util.*;

import java.util.NoSuchElementException;
import java.util.TreeSet;
import java.util.Vector;

public class StaticEdgeIterator {
	private StaticGraph graph;

	private int nextNode = 0;
	private int nextLink = 0;
	
	private boolean done = false;
	
	private Vector<String> nodes = new Vector<String>();
	
	public StaticEdgeIterator(StaticGraph g) {
		graph = g;
		Vector<String> graphNodes = graph.getNodes();
		for(int i = 0; i < graphNodes.size(); i++) {
			if(! graph.getNbrs(graphNodes.get(i)).isEmpty()) {
				nodes.add(graphNodes.get(i));
			}
		}
		if(nodes.size() == 0) {
			done = true;
		}
	}
	
	public Object[] next() {
		if(done)
			throw new NoSuchElementException();
		String node = nodes.get(nextNode);
		String otherNode = graph.getNbrs(node).get(nextLink);
		Double link = graph.getEdge(node, otherNode);
		if((++nextLink) >= graph.getNbrs(node).size()) {
			nextLink = 0;
			if((++nextNode) >= nodes.size()) {
				done = true;
			}
		}

		Object[] results = { node, otherNode, link };
		return results;
	}
	
	public boolean hasNext() {
		return ! done;
	}
	
}
